<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2022-8-9</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        h1 {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100%;
            height: 60px;
            color: #000;
            transition: top .5s linear;
         /* 过渡语法     上 0.5秒 线性过渡*/
            position: fixed;
            /* fixed生成绝对定位的元素 相对于浏览器窗口的定位 */
            top: -60px;
        }

        button {
            transition: bottom .5s linear;
            /* 过渡语法   底部 0.5秒  线性过渡 */
            position: fixed;
            right: 30px;
            bottom: -30px;
        }

        body {
            height: 3000px;
        }
    </style>
</head>

<body>
    <h1 id="myH1">你好世界</h1>
    <h2>1</h2>
    <h2>2</h2>
    <h2>3</h2>
    <h2>4</h2>
    <h2>5</h2>
    <h2>6</h2>
    <h2>7</h2>
    <h2>8</h2>
    <h2>9</h2>
    <h2>10</h2>
    <h2>11</h2>
    <h2>12</h2>
    <h2>13</h2>
    <h2>14</h2>
    <h2>15</h2>
    <h2>16</h2>
    <h2>17</h2>
    <h2>18</h2>
    <h2>19</h2>
    <h2>20</h2>
    <h2>21</h2>
    <h2>22</h2>
    <h2>23</h2>
    <h2>24</h2>
    <h2>25</h2>
    <h2>26</h2>
    <h2>27</h2>
    <h2>28</h2>
    <h2>29</h2>
    <h2>30</h2>
    <h2>31</h2>
    <h2>32</h2>
    <h2>33</h2>
    <h2>34</h2>
    <h2>35</h2>
    <h2>36</h2>
    <h2>37</h2>
    <h2>38</h2>
    <h2>39</h2>
    <h2>40</h2>
    <h2>41</h2>
    <h2>42</h2>
    <h2>43</h2>
    <h2>44</h2>
    <h2>45</h2>
    <h2>46</h2>
    <h2>47</h2>
    <h2>48</h2>
    <h2>49</h2>
    <h2>50</h2>
    <button id="go">我是按钮</button>



    <script>
        go.onclick = function () {
            //点击这个按钮
            window.scrollTo({
                top: 0,
                behavior: 'smooth',//平滑过渡

            })
        }
        // console.dir(myH1);
        //滚动条触发事件
        window.onscroll = function () {
            // 获取滚动条位置 在去判断滚动条向下滚动多少
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop
            //判断scrollTop向下滚动了多少 
            if (scrollTop >= 300) {
                myH1.style.top = '0px'//显示顶部你好世界
                myH1.style.background = 'blue'//显示之后修改颜色的样式
                go.style.bottom = '30px'//显示底部按钮
            } else {
            //判断语句不走if 就走这个else 不走if滚动条肯定不大于三百
            //不大于三百就再把他们隐藏
                myH1.style.top = '-60px' 
                go.style.bottom = '-30px'
            }
        }

    </script>
</body>

</html>